package ex.recruit.action;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;

import com.guinea.core.PropertyFilterForSql;
import com.guinea.core.utils.ServletUtils;
import com.guinea.core.web.StrutsAction;

import ex.recruit.service.ApplyBaseServiceEx;

public class ApplyRegionReportAction extends StrutsAction {
	protected ApplyBaseServiceEx applyBaseServiceEx;

	public ApplyBaseServiceEx getApplyBaseServiceEx() {
		return applyBaseServiceEx;
	}

	public void setApplyBaseServiceEx(ApplyBaseServiceEx applyBaseServiceEx) {
		this.applyBaseServiceEx = applyBaseServiceEx;
	}

	/**
	 * url参数未定义method时的默认Action函数. 默认为index Action.
	 */
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return list(mapping, form, request, response);
	}

	/**
	 * 列出所有对象的Action函数.
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 从request中获取含属性前缀名的参数,构造去除前缀名后的参数Map.
		Map filterParamMap = ServletUtils.getParametersStartingWith(request,
				"filter_");
		List filters = PropertyFilterForSql.buildFromParaMap(filterParamMap);
		String rsscId = PropertyFilterForSql.getProperty("rsscId", filters);
		String actflag=request.getParameter("actflag");
		if(actflag==null||"".equals(actflag)){
			actflag="searchRbo";
		}
		JFreeChart j = null;
		List chartList=null;
		if("searchRbo".equals(actflag)){			
			request.setAttribute("hasManyRbo", "1");
			chartList= getApplyBaseServiceEx().getRboApplyAuditProcess(filters);
			j = createChart(createRboDataset(chartList));
		}else{			
			request.setAttribute("hasManyRbo", "0");
			chartList= getApplyBaseServiceEx().getProvinceApplyAuditPrcocess(filters);
			j = createChart(createProvinceDataset(chartList));
		}

		request.setAttribute("result", chartList);
		request.setAttribute("filters", filterParamMap);
		
		String filename = null;
		try {
			filename = ServletUtilities.saveChartAsPNG(j, 700, 300, request
					.getSession());
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (filename == null)
			filename = "";
		request.setAttribute("filename", filename);
		return mapping.findForward("list");
	}

	private CategoryDataset createRboDataset(List data) {
		DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
		//test 2109999
		for (int i=0;i<data.size();i++){
			Map dtmap=(Map)data.get(i);
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("confirm_amount")),
					"申请确认", String.valueOf(dtmap.get("vname")));
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("first_trial_amount")),
					"初审", String.valueOf(dtmap.get("vname")));
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("visit_amount")),
					"走访",  String.valueOf(dtmap.get("vname")));
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("mreview_amount")),
					"材料审核",  String.valueOf(dtmap.get("vname")));
			
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("skn_audit_amount")),
					"SKN内审",  String.valueOf(dtmap.get("vname")));
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("final_judgment_amount")),
					"决策委员会审批",  String.valueOf(dtmap.get("vname")));		
		}		
		return localDefaultCategoryDataset;
	}

	private CategoryDataset createProvinceDataset(List data) {
		DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
		
		for (int i=0;i<data.size();i++){
			Map dtmap=(Map)data.get(i);
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("confirm_amount")),
					"申请确认", String.valueOf(dtmap.get("vprovname")));
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("first_trial_amount")),
					"初审", String.valueOf(dtmap.get("vprovname")));
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("visit_amount")),
					"走访",  String.valueOf(dtmap.get("vprovname")));
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("mreview_amount")),
					"材料审核",  String.valueOf(dtmap.get("vprovname")));
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("skn_audit_amount")),
					"SKN内审",  String.valueOf(dtmap.get("vprovname")));
			localDefaultCategoryDataset.addValue(Integer.parseInt((String)dtmap.get("final_judgment_amount")),
					"决策委员会审批",  String.valueOf(dtmap.get("vprovname")));		
		}		
		return localDefaultCategoryDataset;
	}

	private JFreeChart createChart(CategoryDataset paramCategoryDataset) {		
		JFreeChart localJFreeChart = ChartFactory.createStackedBarChart(
				"区域统计报表", "", "数量", paramCategoryDataset,
				PlotOrientation.VERTICAL, true, true, false);		
		
//		 配置字体
		Font xfont = new Font("宋体", Font.PLAIN, 12);// X轴
		Font yfont = new Font("宋体", Font.PLAIN, 12);// Y轴
		Font kfont = new Font("宋体", Font.PLAIN, 12);// 底部
		Font titleFont = new Font("隶书", Font.BOLD, 25); // 图片标题
		CategoryPlot plot = localJFreeChart.getCategoryPlot();// 图形的绘制结构对象
		
 		plot.setBackgroundPaint(Color.lightGray);
		plot.setRangeGridlinePaint(Color.white);
		
		StackedBarRenderer stackedBarRenderer = (StackedBarRenderer)plot.getRenderer();
		stackedBarRenderer.setSeriesPaint(0, new Color(220, 220, 220));
		stackedBarRenderer.setSeriesPaint(1, new Color(190, 190, 190));
		stackedBarRenderer.setSeriesPaint(2, new Color(110, 110, 110));
		stackedBarRenderer.setSeriesPaint(3, new Color(219, 238, 213));
		stackedBarRenderer.setSeriesPaint(4, new Color(129, 194, 109));
		stackedBarRenderer.setSeriesPaint(5, new Color(75, 168, 46));
		
		
		stackedBarRenderer.setDrawBarOutline(false);
		//stackedBarRenderer.setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator());
		stackedBarRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		
		stackedBarRenderer.setBaseItemLabelsVisible(true);
		
		plot.setNoDataMessage("没有数据!");
		plot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 25));
		plot.setNoDataMessagePaint(Color.RED);
		
		/*stackedBarRenderer
				.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		stackedBarRenderer
				.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());*/
		stackedBarRenderer.setMaximumBarWidth(0.03d); //柱状宽度
		plot.setRenderer(stackedBarRenderer);
		plot.setBackgroundPaint(Color.WHITE);
		
		// 图片标题
		TextTitle localTextTitle = localJFreeChart.getTitle();
		//localTextTitle.setBorder(0.0D, 0.0D, 1.0D, 0.0D);
		//localTextTitle.setBackgroundPaint(new GradientPaint(0.0F, 0.0F,
		//		Color.lightGray, 350.0F, 0.0F, Color.white, true));
		localTextTitle.setExpandToFitSpace(true);
		localTextTitle.setFont(titleFont);

		// 底部
		localJFreeChart.getLegend().setItemFont(kfont);
		localJFreeChart.getLegend().setHorizontalAlignment(
				HorizontalAlignment.CENTER);

		// X 轴

		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setLabelFont(xfont);// 轴标题
		domainAxis.setTickLabelFont(xfont);// 轴数值
		domainAxis.setTickLabelPaint(Color.BLUE); // 字体颜色
		domainAxis.setAxisLineVisible(false);
		// Y 轴
		ValueAxis rangeAxis = plot.getRangeAxis();
		rangeAxis.setLabelFont(yfont);
		rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色
		rangeAxis.setTickLabelFont(yfont);
		rangeAxis.setAxisLineVisible(false);
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		
		
		
		return localJFreeChart;
	}

}
